$(function () {
    $('#deviceCode').focus();
    //重置表单
    $("#searchReset").on("click",function(){
        $("#searchForm")[0].reset();
    });
    //渲染表格
    var columns = deviceList.columnsInit();
    var table = deviceList.gridInit(columns);
    var datas = deviceList.dataInit();
    $("#deviceTable").bootstrapTable("append",datas);
    //点击删除按钮信息弹出框
    $("#del-btn").click(function(){
        var len = $("#deviceTable input:checked").length;
        if (len == 0) {
            //未选中数据提示框
            layer.msg('请选择数据',  {time:1000});
        } else {
            layer.confirm('确定要删除这条数据吗？',
                {icon: 3, title: '提示', area: '300px', btnAlign: 'c'}, function (index) {
                    layer.close(index);
                });
        }

    });
    //新建弹框
    $("#add-btn").on('click',function () {
        layer.open({
            move: false,
            type: 2,
            title: '新建',
            area: ['800px', '500px'],
            content: 'j_device_add.html',
            btn: ['保存','取消'],
            btnAlign: 'c',
            yes: function (index, layero) {
                var openWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                var form= openWin.submitForm();
                if(!form){
                    layer.close(index);
                }
            }, cancel: function (index) {
                layer.close(index);
            }
        });
    })
    //缓存数据弹框
    $("#viewData").on('click',function () {
        layer.open({
            move: false,
            type: 2,
            title: '缓存数据',
            area: ['800px', '500px'],
            content: 'j_view_cache_data.html',
            btn: ['关闭'],
            btnAlign: 'c',
            yes: function (index, layero) {
                layer.close(index);
            }, cancel: function (index) {
                layer.close(index);
            }
        });
    })
})
;(function(undefined) {
    "use strict"
    var _global;

    function result(args,fn){
        var argsArr = Array.prototype.slice.call(args);
        if(argsArr.length > 0){
            return argsArr.reduce(fn);
        } else {
            return 0;
        }
    }
    var deviceList = {
        columnsInit:function(){
            return  [{
                field: 'od',
                title: '序号',
                width:'2%',
                align:'center'
            }, {
                field: 'code',
                title: '设备编码',
                sortable : true
            }, {
                field: 'name',
                title: '设备名称'
            },{
                field: 'service',
                title: '协议服务',
            }, {
                field: 'address',
                title: '安装地址'
            }, {
                field: 'number',
                title: '排序号'
            }, {
                field: 'status',
                title: '状态',
                formatter: function (value, row, index) {
                    if(value == '正常'){
                        return [
                            '<span class="sys-type-bg pro-status-green">正常</span>'
                        ];
                    }
                    return [
                        '<span class="sys-type-bg pro-status-red">停用</span>'
                    ];

                }

            }, {
                field: 'date',
                title: '最后一次时间'

            }, {
                field: 'optionDel',
                checkbox: true
            },{
                field: 'option',
                title: '操作',
                width: 80,
                align: 'center',
                formatter: function (value, row, index) {
                    /*//up菜单向上展示、 down向下展示
                    var pageSize = deviceList.pageSize;
                    var cssStyle = 'bottom';
                    if (index > pageSize / 3) {
                        cssStyle = 'top';
                    }*/
                    return [
                        '<div class="opt-dropdown tb-opt-div"><a class="tb-opt-icon" href="javascript:void(0)">操作 <i class="glyphicon glyphicon-triangle-bottom"></i></a>',
                        '<ul class="opt-dropdown-menu opt-dropdown-menu-bottom">',
                        '<li><a href="javascript:void(0)" title="编辑">',
                        '<i class="glyphicon glyphicon-pencil"></i><em> 编辑</em></a></li>',
                        '<li><a href="javascript:void(0)"><i class="glyphicon glyphicon-cloud-download"></i><em> 获取日志</em></a></li></ul></div>'
                    ].join('');
                }

            }]
        },
        gridInit:function(columns){
            return $("#deviceTable").bootstrapTable({
                url:'',
                columns: columns,
                striped: 'true',
                classes: 'table table-hover qxttable',
                pagination: 'true',
                pageNumber: 1,
                pageSize: 10,
                pageList: [
                    10, 30, 50, 100, 200
                ],
                onPostBody:function () {
                    $(".opt-dropdown").each(function(index,data){
                        var _this = this;
                        $(this).hover(function(){
                            $(_this).find(".opt-dropdown-menu").show();
                        },function(){
                            $(_this).find(".opt-dropdown-menu").hide();
                        })
                    });
                }
            })

        },
        dataInit:function(){
            return  [{
                od: 1,
                code: '234242424234243',
                name: '李某某',
                service:'2017/05/12 15:52:12',
                address:'234242424234243',
                number:'塑料杯1',
                status:'正常',
                date:'2017/05/12 15:52:12'
            }, {
                od: 2,
                code: '234242424234243',
                name: '李某某',
                service:'2017/05/12 15:52:12',
                address:'234242424234243',
                number:'塑料杯1',
                status:'停用',
                date:'2017/05/12 15:52:12'
            },{
                od: 3,
                code: '234242424234243',
                name: '李某某',
                service:'2017/05/12 15:52:12',
                address:'234242424234243',
                number:'塑料杯1',
                status:'正常',
                date:'2017/05/12 15:52:12'
            },{
                od: 4,
                code: '234242424234243',
                name: '李某某',
                service:'2017/05/12 15:52:12',
                address:'234242424234243',
                number:'塑料杯1',
                status:'正常',
                date:'2017/05/12 15:52:12'
            },{
                od: 5,
                code: '234242424234243',
                name: '李某某',
                service:'2017/05/12 15:52:12',
                address:'234242424234243',
                number:'塑料杯1',
                status:'正常',
                date:'2017/05/12 15:52:12'
            },{
                od: 6,
                code: '234242424234243',
                name: '李某某',
                service:'2017/05/12 15:52:12',
                address:'234242424234243',
                number:'塑料杯1',
                status:'正常',
                date:'2017/05/12 15:52:12'
            },{
                od: 7,
                code: '234242424234243',
                name: '李某某',
                service:'2017/05/12 15:52:12',
                address:'234242424234243',
                number:'塑料杯1',
                status:'正常',
                date:'2017/05/12 15:52:12'
            },{
                od: 8,
                code: '234242424234243',
                name: '李某某',
                service:'2017/05/12 15:52:12',
                address:'234242424234243',
                number:'塑料杯1',
                status:'正常',
                date:'2017/05/12 15:52:12'
            },{
                od: 9,
                code: '234242424234243',
                name: '李某某',
                service:'2017/05/12 15:52:12',
                address:'234242424234243',
                number:'塑料杯1',
                status:'正常',
                date:'2017/05/12 15:52:12'
            },{
                od: 10,
                code: '234242424234243',
                name: '李某某',
                service:'2017/05/12 15:52:12',
                address:'234242424234243',
                number:'塑料杯1',
                status:'正常',
                date:'2017/05/12 15:52:12'
            },{
                od: 11,
                code: '234242424234243',
                name: '李某某',
                service:'2017/05/12 15:52:12',
                address:'234242424234243',
                number:'塑料杯1',
                status:'正常',
                date:'2017/05/12 15:52:12'
            },{
                od: 12,
                code: '234242424234243',
                name: '李某某',
                service:'2017/05/12 15:52:12',
                address:'234242424234243',
                number:'塑料杯1',
                status:'正常',
                date:'2017/05/12 15:52:12'
            },{
                od: 13,
                code: '234242424234243',
                name: '李某某',
                service:'2017/05/12 15:52:12',
                address:'234242424234243',
                number:'塑料杯1',
                status:'正常',
                date:'2017/05/12 15:52:12'
            }]
        }
    }

    // 将插件对象暴露给全局对象
    _global = (function(){ return this || (0, eval)('this'); }());
    if (typeof module !== "undefined" && module.exports) {
        module.exports = deviceList;
    } else if (typeof define === "function" && define.amd) {
        define(function(){return deviceList;});
    } else {
        !('plugin' in _global) && (_global.deviceList = deviceList);
    }
}());